﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;

/// <summary>
/// Summary description for BlogLinqProvider
/// </summary>
/// 
namespace com.ClickCricket.dal
{
    public class BlogLinqProvider : IBlogProvider
    {
        public BlogLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public List<Blog> getBlogs()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var blogs = from aBlog in dc.Blogs
                            select aBlog;

                if (blogs == null)
                {
                    return null;
                }
                List<Blog> _blogList = new List<Blog>();
                foreach (var blog in blogs)
                {
                    _blogList.Add(extractBlog(blog));
                }

                return _blogList;
            }
            
        }

        public void SetBlog(Blog blg)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                dc.Blogs.InsertOnSubmit(createEntity(blg));
                dc.SubmitChanges();
            }
        }

        private Entity.Blog createEntity(Blog blg)
        {
            Entity.Blog b = new Entity.Blog();
            b.BlogID = blg.BlogID;
            b.Data = blg.Data;
            b.Date = blg.Date;
            b.Title = blg.Title;

            return b;
        }

        private Blog extractBlog(Entity.Blog blog)
        {
            Blog aBlog = new Blog(
                blog.BlogID,
                blog.Title,
                blog.Data,
                Convert.ToDateTime(blog.Date));

            return aBlog;

        }
    }
}
